package com.yy.job.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Preconditions;
import com.yy.job.common.dao.YYJobDOMapper;
import com.yy.job.common.dao.YYJobStepDOMapper;
import com.yy.job.common.dao.po.YYJobDO;
import com.yy.job.common.dao.po.YYJobStepDO;
import com.yy.job.common.utils.BaseResult;
import com.yy.job.core.config.JobConfigXmlContext;
import com.yy.job.core.config.xml.JobStepXmlConfig;
import com.yy.job.core.config.xml.JobXmlConfig;
import com.yy.job.core.domain.JobResult;
import com.yy.job.core.domain.JobStepDomain;
import com.yy.job.core.run.process.IJobStepProcess;
import com.yy.job.dto.JobStepDTO;
import com.yy.job.spring.YYSpringBeanUtils;

/**
 * 后门，执行step
 * 
 * @author eleven.li
 *
 */
@RestController
@RequestMapping({ "/api/job" })
public class JobCoreAPI {
	
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private YYJobDOMapper jobDOMapper;
	
	@Autowired
	private YYJobStepDOMapper jobStepDOMapper;
	
	@Autowired
	private JobConfigXmlContext jobConfigXmlContext;
	
	/**
	 * 后门，仅用于触发step，不涉及到 状态，执行时间的变更
	 * 
	 * @param jobStepDTO
	 * @return
	 */
	@RequestMapping(value = { "/executeJobStep" }, method = { RequestMethod.POST })
	public BaseResult<JobResult> executeJobStep(@RequestBody JobStepDTO jobStepDTO) {
		try {
			Preconditions.checkArgument(jobStepDTO != null, "参数不能为空");
			Preconditions.checkArgument(jobStepDTO.getId() != null, "id不能为空");
			Preconditions.checkArgument(jobStepDTO.getOperator() != null, "operator不能为空");
			
			YYJobStepDO jobStepDO = jobStepDOMapper.selectByPrimaryKey(jobStepDTO.getId());
			Preconditions.checkArgument(jobStepDTO != null, "查询步骤数据为空");
			
			YYJobDO jobDO = this.jobDOMapper.selectByPrimaryKey(jobStepDO.getJobId());
			Preconditions.checkArgument(jobStepDTO != null, "查询任务数据为空");
			
			Preconditions.checkArgument(jobConfigXmlContext.getProjectXmlConfig().getProjectCode().equals(jobDO.getProjectCode()), "projectCode不一致");
			
			JobXmlConfig jobXmlConfig = jobConfigXmlContext.getProjectXmlConfig().findJobConfig(jobDO.getModuleCode(), jobDO.getJobCode());
			Preconditions.checkArgument(jobXmlConfig != null, "jobXmlConfig为空");
			
			JobStepXmlConfig jobStepXmlConfig = jobXmlConfig.findJobStepConfig(jobStepDO.getStepCode());
			Preconditions.checkArgument(jobStepXmlConfig != null, "jobStepXmlConfig为空");
			
			IJobStepProcess jobStepProcess = YYSpringBeanUtils.getJobStepProcess(jobStepXmlConfig.getStepProcessBean());
			JobResult jobResult = jobStepProcess.execute(new JobStepDomain(jobStepDO), jobStepDO.getStepParams());
			
			return BaseResult.success(jobResult);
		}
		catch(IllegalArgumentException ex) {
			log.warn("executeJobStep failed", ex);
			return BaseResult.fail("20001", ex.getMessage());
		}
		catch(Exception ex) {
			log.error("executeJobStep failed", ex);
			return BaseResult.fail("20001", "系统异常");
		}
	}
	
}
